<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\admin\logic\finance;

use app\basic\BaseLogic;
use exception\ApiException;
use support\utils\Helper;
use app\admin\model\finance\UserMoneyLog;

/**
 * 余额记录逻辑层
 */
class UserMoneyLogLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserMoneyLog();
    }


    /**
     * 增加余额变更记录
     * @param $user_id
     * @param $money
     * @param $originMoney
     * @param $afterMoney
     * @param $title
     * @param $memo
     * @return UserMoneyLog|\think\Model
     */
    public static  function createLog($user_id,$money,$originMoney,$afterMoney,$title,$memo)
    {
        return  UserMoneyLog::create([
            'user_id' => $user_id,
            'money' => $money,
            'before' => $originMoney,
            'after' => $afterMoney,
            'title' => $title,
            'memo' => $memo,
        ]);
    }

    /**
     * 获取记录数据
     */
    public function getDataList($query)
    {
        $page = request()->input('page', 1);
        $limit = request()->input('limit', 10);
        $orderBy = request()->input('orderBy', '');
        $orderType = request()->input('orderType', $this->orderType);
        if ($this->scope) {
            $query = $this->userDataScope($query);
        }
        if(empty($orderBy)) {
            $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
        }
        $query->order($orderBy, $orderType);
        return $query->with('user')->paginate($limit, false, ['page' => $page])->toArray();
    }


}
